package com.sky.service.impl;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 将商品添加进购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 将 传参和userId 封装shoppingcart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断购物车中是否存在该商品
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        // 添加进数据库
        // 判断存在就+1 不存见就添加
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            // 说明有商品 number+1
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updatNumberById(shoppingCart);
        } else {
            //说明没有商品 判断为套餐或是菜品
            Long dishId = shoppingCart.getDishId();
            // 条件成立则为菜品
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);

        }
    }

    /**
     * 查询列表
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        // 组装shoppingCart对象
        ShoppingCart shoppingCart = ShoppingCart
                .builder()
                .userId(BaseContext.getCurrentId())
                .build();
        // 调用mapper层，响应数据
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车单个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {

        // 将 传参和userId 封装shoppingcart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 判断购物车中是否存在该商品
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        ShoppingCart shopping = shoppingCarts.get(0);
        if (shopping.getNumber() > 1) {
            //说明商品数量大于1存在
            shopping.setNumber(shopping.getNumber() - 1);
            shoppingCartMapper.updatNumberById(shopping);
        } else {
            //说明数量等于1
            //判断是否为菜品性别
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                shoppingCartMapper.deleteByDishId(userId,dishId);
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                shoppingCartMapper.deleteBySetmealId(userId, setmealId);
            }
        }
    }
}
